﻿package com.petermacko.utils {
	import flash.utils.*;
	
	/**
	 * The CacheItem class is used internally to a Cache instance. Each item
	 * in a Cache that is stored, is wrapped by this CacheItem instance to allow
	 * for expirations at an object level.
	 * 
	 * <p>This class does not need to be used in order to store something in a Cache.<p>
	 * 
	 * @see com.petermacko.utils.Cache
	 */
	final public class CacheItem
	{
		private var purgeCallback:Function; //The callback to call when this item expires.

		public var timeout:int; //The timeout to expire.
		public var object:*; //The stored data.
		public var cacheKey:String; //The key that was used in the actual cache.

		public function CacheItem(key:String,object:*,purgeCallback:Function,timeout:int=-1):void {
			this.purgeCallback = purgeCallback;
			cacheKey = key;
			this.timeout = timeout;
			this.object = object;
			if(timeout>-1) flash.utils.setTimeout(purgeItem, timeout);
		}
		
		//On timeout, the item is expired.
		private function purgeItem():void {
			this.purgeCallback(cacheKey);
		}

		//Destroy this cache items internal variables.
		public function dispose():void {
			purgeCallback = null;
			cacheKey = null;
			flash.utils.clearTimeout(timeout);
			timeout = NaN;
			object = null;
		}
	}
}